# 积分购买流程
from time import sleep

from selenium.webdriver import ActionChains

from pages.client_page.goods_page import GoodsPage
from pages.client_page.integral_page import IntegralPage
from utils.object_utils import cache


class IntegralLogic():
    # 积分购买
    def buy(self, driver):
        # 点击我的积分链接
        driver.find_element(*IntegralPage.integral_link_elem).click()
        sleep(2)
        # 点击积分换购按钮
        driver.find_element(*IntegralPage.integral_btn_elem).click()
        sleep(3)
        # 点击立即换购按钮
        driver.find_elements(*IntegralPage.buy_now_elem)[0].click()
        driver.implicitly_wait(10)

        # 获取商品名称，商品单价，商品数量
        good_name = driver.find_element(*GoodsPage.good_name).text
        cache.set("good_name", good_name[0:13])

        good_price = driver.find_element(*GoodsPage.good_price).text
        cache.set("good_price", good_price)

        good_num = driver.find_element(*GoodsPage.good_num).get_attribute("value")
        cache.set("good_num", good_num)

        # 点击立即兑换按钮
        driver.find_element(*IntegralPage.join_cart_now).click()
        sleep(10)
        driver.find_element(*IntegralPage.pay_pwd).send_keys("123456")
        # 点击提交订单
        driver.find_element(*IntegralPage.submit).click()
        # 选择支付方式
        driver.find_element(*IntegralPage.pay).click()
        # 点击确认支付方式
        driver.find_element(*IntegralPage.confirm_pay).click()
        # 鼠标悬停在我的商场链接上
        ActionChains(driver).move_to_element(driver.find_element(*IntegralPage.my_shop)).perform()
        sleep(1)
        # 点击我的订单
        driver.find_element(*IntegralPage.my_order).click()
        driver.implicitly_wait(5)



